// components/Player/Player.js
Component({

    lifetimes:{
        attached(){
            this.getSongDetail()
            this.watchSong()
        }
    },
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        id:wx.getStorageSync('Music'),
        song:{},
        music:'',
        play:false,
        show:false,
        loop:1,
        songlist:[],
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 监听歌曲变化
        watchSong(){
            setInterval(()=>{
                if(this.data.id != wx.getStorageSync('Music')){
                    this.setData({
                        id:wx.getStorageSync('Music'),
                        play:true
                    })
                    this.getSongDetail()
                }
            },200)
        },
        // 获取歌曲详情
        async getSongDetail(){
            const res = await wx.p.request({
                url:`${wx.root}/song/detail`,
                data:{
                    ids:this.data.id,
                }
            })
            this.setData({
                song:res.data.songs[0],
            })
            this.getSong()
        },
        // 获取歌曲url
        async getSong(){
            const res = await wx.p.request({
                url:`${wx.root}/song/url`,
                data:{
                    id:this.data.id
                }
            })
            this.setData({
                music:res.data.data[0].url
            }) 
            
            this.pause()
        },
        // 改变播放状态
        changePlayState(){
            if(this.data.play){
                this.pause()
            }else{
                this.play()
            }
        },
        // 播放歌曲
        play(){
            const backgroundAudioManager = wx.getBackgroundAudioManager()
            backgroundAudioManager.title = this.data.song.name
            backgroundAudioManager.epname = this.data.song.al.name
            backgroundAudioManager.singer = this.data.song.ar[0].name
            backgroundAudioManager.src=this.data.music
            backgroundAudioManager.play()
            this.setData({
                play:true,
            })
           
        },
        // 暂停播放
        pause(){
            const backgroundAudioManager = wx.getBackgroundAudioManager()
            backgroundAudioManager.pause()
            this.setData({
                play:false
            })
        },
        // 打开我的歌曲列表
        showList(){
            this.getSonglist()
            this.setData({
                show:true
            })
            
        },
        // 关闭我的歌曲列表
        onClose(){
            this.setData({
                show:false
            })
        },
        // 改变播放循环状态
        changeLoop(){
            console.log(1);
            let timer2
            clearTimeout(timer2)
            wx.hideToast({
            success: (res) => {},
            })
            this.setData({
                loop:(this.data.loop+1)%3
            })
            let title
            if(this.data.loop == 1) title="循环播放"
            else if(this.data.loop == 2) title="随机播放"
            else title="单曲循环"
            wx.showToast({
            title: title,
            })
            timer2 = setTimeout(() => {
                wx.hideToast({
                success: (res) => {},
                })    
            }, 1000);
            
        },
        // 获取歌曲列表
        async getSonglist(){
            const list = wx.getStorageSync('Songlist') || []
            let s = ""
            for (let i = 0; i < list.length; i++) {
                const item = list[i];
                s += item
                if(i != list.length-1) s += ","
            }
            const res = await wx.p.request({
                url:`${wx.root}/song/detail`,
                data:{
                    ids:s
                }
            })
            this.setData({
                songlist:res.data.songs
            })

        },
        // 删除歌曲
        deleteMusic(e){
            let index = e.currentTarget.dataset.index
            const list = wx.getStorageSync('Songlist')
            list.splice(index,1)
            if(list.length == 0){
                this.setData({
                    songlist:[]
                })
                wx.setStorageSync('Songlist', [])
            }else{
                wx.setStorageSync('Songlist', list)
                this.getSonglist()    
            }
            
        },
        // 切换歌曲
        changeMusic(e){
            let id = e.currentTarget.dataset.id
            wx.setStorageSync('Music', id)
            if(id == this.data.id) return
            this.setData({
                id:id
            })
            this.getSongDetail()
            setTimeout(() => {
                this.play()
            }, 500);
        },
        // 进入歌曲详情页
        GoSong(){
            wx.navigateTo({
              url: `/pages/Song/Song?id=${this.data.id}`,
            })
        }
    }
})
